﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using A5Lib;

namespace A5Site
{
    public partial class User : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Context != null && Context.Session != null)
            {

                if (Session["myuser"] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                A5Lib.User u;
                u = (A5Lib.User)Session["myuser"];//wait for login page to work
                List<A5Lib.Appliance> listOfAppliances = u.Appliances;
                applianceListBox.Items.Clear();

                if (listOfAppliances != null)
                {

                    foreach (A5Lib.Appliance str in u.Appliances)
                    {
                        UserApplianceControl uac = (UserApplianceControl)LoadControl("UserApplianceControl.ascx");
                        uac.setAppliance(str);
                        Panel1.Controls.Add(uac);
                        applianceListBox.Items.Add(str.Name);
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //for when login page works.
            if (Session["myuser"] == null)
            {
                Response.Redirect("Home.aspx");
            }
            A5Lib.User usr = (A5Lib.User)Session["myuser"];
            UserServices.UserServicesClient usc = new UserServices.UserServicesClient();
            if (!usc.ValidateUser(usr.Username, usr.Password))
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void addApplianceButton_Click(object sender, EventArgs e)
        {
            A5Lib.User u;
            UserServices.UserServicesClient myProxy2 = new UserServices.UserServicesClient();
            u = (A5Lib.User)Session["myuser"];
            string textBoxInput = this.AddApplianceBox.Text;
            u.addAppliance(textBoxInput);
            Response.Redirect(Request.RawUrl);
        }

        protected void removeApplianceButton_Click(object sender, EventArgs e)
        {
            if (applianceListBox.SelectedIndex < 0) return;
            try{
                A5Lib.User u;
                u = (A5Lib.User)Session["myuser"];
                string applianceToDelete = applianceListBox.SelectedValue;
                applianceListBox.Items.RemoveAt(applianceListBox.SelectedIndex);
                if(u.deleteAppliance(applianceToDelete) == true)
                {
                    removeApplianceError.Text = "Appliance removed successfully";
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    removeApplianceError.Text = "Appliance removal unsuccessful";
                }
            }
            catch (Exception ex)
            {
                removeApplianceButton.Text = ex.Message;
            }

        }

    }
}